package com.company.gobang.module.room;

import com.company.gobang.module.room.domain.Room;
import com.company.gobang.proto.PlayChessReq;
import xyz.noark.core.annotation.Service;
import xyz.noark.core.network.Session;
import xyz.noark.log.Logger;
import xyz.noark.log.LoggerFactory;

/**
 * @author 小流氓[176543888@qq.com]
 */
@Service
public class RoomService {
    private static final Logger logger = LoggerFactory.getLogger(RoomService.class);

    /**
     * 房间开始事件.
     */
    public void handleRoomStartEvent(int roomId) {
        Room room = RoomManager.getInstance().getRoom(roomId);
        // 1. 通知双方，进入房间
        room.noticePlayerEnterRoom();
        // 2. 随机出一个先手
        room.randomFirstPlayer();


    }

    /**
     * 落子请求
     */
    public void playChessReq(String playerId, Session session, PlayChessReq req) {
        logger.debug("下棋 playerId={}, x={}, y={}", playerId, req.getX(), req.getY());

        Integer roomId = session.attr(Room.attrKey).get();
        Room room = RoomManager.getInstance().getRoom(roomId);

        // 同步给对方
        room.syncPlayChessReq(playerId, req);
    }
}
